/**
 * The single player mode package of the application.
 */
package fr.umlv.m2.android.quizz.singleplayer;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import fr.umlv.m2.android.quizz.R;
import fr.umlv.m2.android.quizz.database.Level;
import fr.umlv.m2.android.quizz.database.LevelDb;
import fr.umlv.m2.android.quizz.database.Theme;
import fr.umlv.m2.android.quizz.database.ThemeDb;

/**
 * This class defines the game initialization of the single player mode activity. 
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class SinglePlayerActivity extends Activity {
	/**
	 * The level spinner.
	 */
	private Spinner levelSpin;
	/**
	 * The theme spinner.
	 */
	private Spinner themeSpin;
	/**
	 * The start game button.
	 */
	private Button startGameButton;
	
	/**
	 * The database level object.
	 */
	private LevelDb levelDb;
	/**
	 * The database theme object.
	 */
	private ThemeDb themeDb;
	
	/**
	 * The level name selected.
	 */
	private String levelName;
	/**
	 * The theme name selected.
	 */
	private String themeName;
	
	/**
	 * The activity intent.
	 */
	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.single_player);
		
		this.levelSpin = (Spinner)findViewById(R.id.singlePlayerInitGameLevelSpinner);
		this.themeSpin = (Spinner)findViewById(R.id.singlePlayerInitGameThemeSpinner);
		this.startGameButton = (Button)findViewById(R.id.singlePlayerStartGameButton);
		
		this.intent = this.getIntent();
		Bundle bundle = this.intent.getExtras();
		final long userId = bundle.getLong("userId");
		
		this.levelDb = new LevelDb(this);
		this.themeDb = new ThemeDb(this);
		
		
		this.initLeveSpinner();
		
		this.levelSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				levelName = (String)parent.getItemAtPosition(position);
				
				levelDb.open();
				Level level = levelDb.getLevelWithName(levelName);
				initThemeSpinner(level.getId());
				levelDb.close();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {}
		});
		
		this.themeSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				themeName = (String)parent.getItemAtPosition(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {}
		});
		
		this.startGameButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				intent = new Intent(SinglePlayerActivity.this, SinglePlayerQuestionActivity.class);
				
				levelDb.open();
				themeDb.open();
				
				intent.putExtra("userId", userId);
				
				Level level = levelDb.getLevelWithName(levelName);
				intent.putExtra("levelId", level.getId());
				Theme theme = themeDb.getThemeWithNameAndLevel(themeName, level.getId());
				intent.putExtra("themeId", theme.getId());
				intent.putExtra("oldQuestions", new long[level.getNbQuestion()]);
				
				levelDb.close();
				themeDb.close();
				
				intent.putExtra("currentQuestionNumber", 1);
				intent.putExtra("goodAnswer", 0);
				intent.putExtra("badAnswer", 0);
				intent.putExtra("time", 0);
				startActivity(intent);
			}
		});
	}
	
	private void initLeveSpinner() {
		this.levelDb.open();
		
		if(this.levelDb.getNbLevel() < 1) {
			throw new IllegalArgumentException("The levels are not initialized.");
		}
		
		List<Level> levels = this.levelDb.getAllLevels();
		List<String> levelsName = new ArrayList<String>();
		for(Level level : levels) {
			levelsName.add(level.getName());
		}
		
		ArrayAdapter<String> levelAdapterSpin = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, levelsName
		);
		
		levelAdapterSpin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		this.levelSpin.setAdapter(levelAdapterSpin);
		this.levelName = (String)this.levelSpin.getSelectedItem();
		
		Level level = this.levelDb.getLevelWithName(levelName);
		this.levelDb.close();
		
		this.initThemeSpinner(level.getId());
	}
	
	private void initThemeSpinner(long levelId) {
		this.themeDb.open();
		
		if(this.themeDb.getNbThemeWithLevel(levelId) < 1) {
			throw new IllegalArgumentException("The themes are not initialized");
		}
		
		List<Theme> themes = this.themeDb.getThemesWithLevel(levelId);
		this.themeDb.close();
		
		List<String> themesName = new ArrayList<String>();
		for(Theme theme : themes) {
			themesName.add(theme.getName());
		}
		
		ArrayAdapter<String> themeAdapterSpin = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, themesName
		);
		
		themeAdapterSpin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		this.themeSpin.setAdapter(themeAdapterSpin);
		this.themeName = (String)this.themeSpin.getSelectedItem();
	}
}
